<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>Linux源码包安装 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>Linux源码包安装</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Linux/">Linux</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在linux下面安装一个源码包是最常用的，在日常的管理工作中，大部分软件都是通过源码安装的。安装一个源码包，是需要自己把源代码编译成二进制的可执行文件。如果读得懂这些源代码，那么就可以去修改这些源代码自定义功能，然后再去编译成想要的。使用源码包的好处除了可以自定义修改源代码外还可以定制相关的功能，因为源码包在编译的时候是可以附加额外的选项的。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;源码包的编译用到了linux系统里的编译器，常见的源码包一般都是用C语言开发的，这也是因为C语言为linux上最标准的程序语言。Linux上的C语言编译器叫做gcc，利用它就可以把C语言变成可执行的二进制文件。所以如果你的机器上没有安装gcc就没有办法去编译源码。你可以使用 yum install -y gcc 来完成安装。</p>
<h2 id="安装一个源码包，通常需要三个步骤："><a href="#安装一个源码包，通常需要三个步骤：" class="headerlink" title="安装一个源码包，通常需要三个步骤："></a>安装一个源码包，通常需要三个步骤：</h2><h3 id="configure"><a href="#configure" class="headerlink" title="./configure"></a>./configure</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在这一步可以定制功能，加上相应的选项即可，具有有什么选项可以通过 ./configure –help 命令来查看。在这一步会自动检测你的linux系统与相关的套件是否有编译该源码包时需要的库，因为一旦缺少某个库就不能完成编译。只有检测通过后才会生成一个Makefile文件。</p>
<h3 id="make"><a href="#make" class="headerlink" title="make"></a>make</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用这个命令会根据Makefile文件中预设的参数进行编译，这一步其实就是gcc在工作了。</p>
<h3 id="make-install"><a href="#make-install" class="headerlink" title="make install"></a>make install</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;安装步骤，生成相关的软件存放目录和配置文件的过程。</p>
<h2 id="Apache源码安装实例"><a href="#Apache源码安装实例" class="headerlink" title="Apache源码安装实例"></a>Apache源码安装实例</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;上面的3步并不是所有的源码包软件都一样的，也就是说源码包的安装并非具有一定的标准安装步骤。这就需要拿到源码包解压后，然后进入到目录找相关的帮助文档，通常会以INSTALL或者README为文件名。所以一定要去看一下。</p>
<h3 id="1-下载一个源码包"><a href="#1-下载一个源码包" class="headerlink" title="1.下载一个源码包"></a>1.下载一个源码包</h3><p>下载源码包一定要去官方站点去下载，不要在网上随便下载，那样很不安全。因为下载到的源码包很有可能是被人修改过的。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@localhost src]<span class="comment"># cd /usr/local/src/</span></div><div class="line">[root@localhost src]<span class="comment"># wget http://mirrors.hust.edu.cn/apache/httpd/httpd-2.2.27.tar.bz2</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下载地址为apache官方网站上提供的一个镜像，下载速度还可以。在下载之前，进入到了 “/usr/local/src” 目录，这是因为习惯把源码包都放到这个目录下，这样做的好处是，方便自己和其他管理员维护，所以以后下载的源码包都统一放到这个目录下吧。</p>
<h3 id="2-解压源码包"><a href="#2-解压源码包" class="headerlink" title="2.解压源码包"></a>2.解压源码包</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@localhost src]<span class="comment"># tar jxvf httpd-2.2.27.tar.bz2</span></div></pre></td></tr></table></figure>
<h3 id="3-配置相关的选项，并生成Makefile"><a href="#3-配置相关的选项，并生成Makefile" class="headerlink" title="3.配置相关的选项，并生成Makefile"></a>3.配置相关的选项，并生成Makefile</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line">[root@localhost src]<span class="comment"># cd httpd-2.2.27</span></div><div class="line">[root@localhost httpd-2.2.27]<span class="comment"># ./configure --help |less</span></div><div class="line">`configure<span class="string">' configures this package to adapt to many kinds of systems.</span></div><div class="line"><span class="string"></span></div><div class="line"><span class="string">Usage: ./configure [OPTION]... [VAR=VALUE]...</span></div><div class="line"><span class="string"></span></div><div class="line"><span class="string">To assign environment variables (e.g., CC, CFLAGS...), specify them as</span></div><div class="line"><span class="string">VAR=VALUE.  See below for descriptions of some of the useful variables.</span></div><div class="line"><span class="string"></span></div><div class="line"><span class="string">Defaults for the options are specified in brackets.</span></div><div class="line"><span class="string"></span></div><div class="line"><span class="string">Configuration:</span></div><div class="line"><span class="string">  -h, --help              display this help and exit</span></div><div class="line"><span class="string">      --help=short        display options specific to this package</span></div><div class="line"><span class="string">      --help=recursive    display the short help of all the included packages</span></div><div class="line"><span class="string">  -V, --version           display version information and exit</span></div><div class="line"><span class="string">  -q, --quiet, --silent   do not print `checking ...'</span> messages</div><div class="line">      --cache-file=FILE   cache <span class="built_in">test</span> results <span class="keyword">in</span> FILE [disabled]</div><div class="line">  -C, --config-cache      <span class="built_in">alias</span> <span class="keyword">for</span> `--cache-file=config.cache<span class="string">'</span></div><div class="line"><span class="string">  -n, --no-create         do not create output files</span></div><div class="line"><span class="string">      --srcdir=DIR        find the sources in DIR [configure dir or `..'</span>]</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用 ./configure –help 命令查看可以使用的选项。一般常用的有 –prefix=PREFIX 这个选项的意思是定义软件包安装到哪里。一个小小的建议，通常源码包都是安装在/usr/local/目录下的。比如，把apache安装在/usr/local/apache2下，那么这里就应该这样写 –prefix=/usr/local/apache2 其他还有好多选项，如果有耐心可以挨个去看一看都有什么作用。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div></pre></td><td class="code"><pre><div class="line">[root@localhost httpd-2.2.27]<span class="comment"># ./configure --prefix=/usr/local/apache2</span></div><div class="line">checking <span class="keyword">for</span> chosen layout... Apache</div><div class="line">checking <span class="keyword">for</span> working mkdir -p... yes</div><div class="line">checking build system <span class="built_in">type</span>... i686-pc-linux-gnu</div><div class="line">checking host system <span class="built_in">type</span>... i686-pc-linux-gnu</div><div class="line">checking target system <span class="built_in">type</span>... i686-pc-linux-gnu</div><div class="line"></div><div class="line">Configuring Apache Portable Runtime library ...</div><div class="line"></div><div class="line">checking <span class="keyword">for</span> APR... reconfig</div><div class="line">configuring package <span class="keyword">in</span> srclib/apr now</div><div class="line">checking build system <span class="built_in">type</span>... i686-pc-linux-gnu</div><div class="line">checking host system <span class="built_in">type</span>... i686-pc-linux-gnu</div><div class="line">checking target system <span class="built_in">type</span>... i686-pc-linux-gnu</div><div class="line">Configuring APR library</div><div class="line">Platform: i686-pc-linux-gnu</div><div class="line">checking <span class="keyword">for</span> working mkdir -p... yes</div><div class="line">APR Version: 1.4.6</div><div class="line">checking <span class="keyword">for</span> chosen layout... apr</div><div class="line">checking <span class="keyword">for</span> gcc... no</div><div class="line">checking <span class="keyword">for</span> cc... no</div><div class="line">checking <span class="keyword">for</span> cl.exe... no</div><div class="line">configure: error: <span class="keyword">in</span> `/usr/<span class="built_in">local</span>/src/httpd-2.2.27/srclib/apr<span class="string">':</span></div><div class="line"><span class="string">configure: error: no acceptable C compiler found in $PATH</span></div><div class="line"><span class="string">See `config.log'</span> <span class="keyword">for</span> more details</div><div class="line">configure failed <span class="keyword">for</span> srclib/apr</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;不幸的是，一开始就报错了，因为没有gcc编译器，需要先安装一下。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@localhost httpd-2.2.27]<span class="comment"># yum install -y gcc</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;由于gcc依赖的包很多，所以安装时间会长一些。安装完后，再继续上面的步骤。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@localhost httpd-2.2.27]<span class="comment"># ./configure --prefix=/usr/local/apache2</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;验证这一步是否成功的命令是：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@localhost httpd-2.2.27]<span class="comment"># echo $?</span></div><div class="line">0</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;返回值如果是 “0” 则执行成功，否则就是没有成功。此时就成功生成 Makefile 了。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@localhost httpd-2.2.27]<span class="comment"># ls -l Makefile</span></div><div class="line">-rw-r--r-- 1 root root 8954 5月  13 12:02 Makefile</div></pre></td></tr></table></figure>
<h3 id="4-进行编译"><a href="#4-进行编译" class="headerlink" title="4.进行编译"></a>4.进行编译</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@localhost httpd-2.2.27]<span class="comment"># make</span></div><div class="line">-bash: make: <span class="built_in">command</span> not found</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;又发生错误了，提示 “make” 命令没有发现，解决办法是安装make工具。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@localhost httpd-2.2.27]<span class="comment"># yum install -y make</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;继续make</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">[root@localhost httpd-2.2.27]<span class="comment"># make</span></div><div class="line">Making all <span class="keyword">in</span> srclib</div><div class="line">make[1]: Entering directory `/usr/<span class="built_in">local</span>/src/httpd-2.2.27/srclib<span class="string">'</span></div><div class="line"><span class="string">Making all in apr</span></div><div class="line"><span class="string">make[2]: Entering directory `/usr/local/src/httpd-2.2.27/srclib/apr'</span></div><div class="line">make[3]: Entering directory `/usr/<span class="built_in">local</span>/src/httpd-2.2.27/srclib/apr<span class="string">'</span></div><div class="line"><span class="string">/bin/sh /usr/local/src/httpd-2.2.27/srclib/apr/libtool --silent --mode=compile gcc -g -O2 -pthread   -DHAVE_CONFIG_H -DLINUX=2 -D_REENTRANT -D_GNU_SOURCE -D_LARGEFILE64_SOURCE   -I./include -I/usr/local/src/httpd-2.2.27/srclib/apr/include/arch/unix -I./include/arch/unix -I/usr/local/src/httpd-2.2.27/srclib/apr/include/arch/unix -I/usr/local/src/httpd-2.2.27/srclib/apr/include  -o passwd/apr_getpass.lo -c passwd/apr_getpass.c &amp;&amp; touch passwd/apr_getpass.lo</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编译的时候，就会出现类似这么多乱七八糟的信息，编译的时间比较长，CPU使用率会很高，这是因为CPU高速计算，编译完后，再使用 echo $? 验证一下是否正常成功。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@localhost httpd-2.2.27]<span class="comment"># echo $?</span></div><div class="line">0</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果是0的话，就可以执行最后一步了。</p>
<h3 id="5-安装"><a href="#5-安装" class="headerlink" title="5.安装"></a>5.安装</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">[root@localhost httpd-2.2.27]<span class="comment"># make install</span></div><div class="line">Making install <span class="keyword">in</span> srclib</div><div class="line">make[1]: Entering directory `/usr/<span class="built_in">local</span>/src/httpd-2.2.27/srclib<span class="string">'</span></div><div class="line"><span class="string">Making install in apr</span></div><div class="line"><span class="string">make[2]: Entering directory `/usr/local/src/httpd-2.2.27/srclib/apr'</span></div><div class="line">make[3]: Entering directory `/usr/<span class="built_in">local</span>/src/httpd-2.2.27/srclib/apr<span class="string">'</span></div><div class="line"><span class="string">make[3]: Nothing to be done for `local-all'</span>.</div><div class="line">make[3]: Leaving directory `/usr/<span class="built_in">local</span>/src/httpd-2.2.27/srclib/apr<span class="string">'</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当然也可以使用 echo $? 看看有没有正确安装，执行完这一步，则会在 “/usr/local/apache2” 目录下增加了很多目录。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@localhost httpd-2.2.27]<span class="comment"># ls /usr/local/apache2/</span></div><div class="line">bin    cgi-bin  error   icons    lib   man     modules</div><div class="line">build  conf     htdocs  include  logs  manual</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;到此，apache源码的安装就完成了</p>

	

	
		<span class="different-posts"><a href="/2017/10/12/1. Linux 基础/40.Linux源码包安装/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
